import 'dart:async';
import 'package:flutter/material.dart';

/**
 * 轮播图
 */
class SimpleCarousel extends StatefulWidget{
  @override
  State<StatefulWidget> createState() => _SimpleCarousel();

}

class _SimpleCarousel extends State<SimpleCarousel>{

  final List<String> images = ['images/ai1.jpg', 'images/ai2.jpg',"images/ai3.jpg"];
  final PageController _controller = PageController();

  @override
  void initState() {
    super.initState();
    // 自动轮播（每3秒切换）
    Timer.periodic(Duration(seconds: 3), (timer) {
      if (_controller.page!.round() < images.length - 1) {
        _controller.nextPage(duration: Duration(milliseconds: 300), curve: Curves.easeIn);
      } else {
        _controller.jumpToPage(0);
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return PageView.builder(
      controller: _controller,
      itemCount: images.length,
      itemBuilder: (context, index) {
        return Image.asset(images[index], fit: BoxFit.cover);
      },
    );
  }

}